package cn.edu.tju.fctl.rest;

import cn.edu.tju.fctl.service.IUserService;
import cn.edu.tju.fctl.service.IVacationService;
import cn.edu.tju.fctl.util.BizCode;
import cn.edu.tju.fctl.util.ExcelUtil;
import cn.edu.tju.fctl.util.ShiroUtil;
import cn.edu.tju.fctl.util.StateCode;
import com.sun.jersey.core.header.ContentDisposition;
import com.sun.jersey.multipart.BodyPart;
import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataParam;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liutong on 15/12/23.
 */
@Component
@Path("file")
public class FileRest {
    private static final String SAVE_FOLDER = System.getProperty("user.dir") + "/fileupload/";
    @Autowired
    IVacationService vacationService;

    @GET
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response getPic(@QueryParam("imgurl") final String fileName) {
        File file = new File(SAVE_FOLDER + fileName);

        Response.ResponseBuilder responseBuilder = Response.ok((Object) file);
        return responseBuilder.build();
    }

    @GET
    @Path("/excel")
    @Produces("application/vnd.ms-excel")
    public Response getExcel(@QueryParam("depart_id") final int depart_id,
                             @QueryParam("fromyear") final int fromyear,
                             @QueryParam("frommonth") @DefaultValue("-1") final int frommonth,
                             @QueryParam("toyear") final int toyear,
                             @QueryParam("tomonth") @DefaultValue("-1") final int tomonth){
        ExcelUtil.generateExcel(vacationService.getDepartmentEvaluation(depart_id,fromyear,frommonth,toyear,tomonth));
        File file = new File(SAVE_FOLDER + "/temp.xls");
        Response.ResponseBuilder responseBuilder = Response.ok((Object) file);
        responseBuilder.header("Content-Disposition", "attachment; filename=evaluation.xls");
        return responseBuilder.build();
    }

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public Object picUpload(@FormDataParam("file") FormDataBodyPart body, @FormDataParam("file1") FormDataBodyPart body1, @FormDataParam("file2") FormDataBodyPart body2){
        List<String> fileUrls = new ArrayList<String>();

        if(body == null){
            return StateCode.buildCode(BizCode.SUCCESS);
        }
        for(BodyPart part : body.getParent().getBodyParts()){
            InputStream is = part.getEntityAs(InputStream.class);
            ContentDisposition meta = part.getContentDisposition();

            try {
                String fileName = String.format("%s", RandomStringUtils.randomAlphanumeric(15));
                OutputStream out = new FileOutputStream(new File(SAVE_FOLDER + fileName));
                byte[] buffer = new byte[1024];
                int bytes = 0;
                while ((bytes = is.read(buffer)) != -1) {
                    out.write(buffer, 0, bytes);
                }
                out.flush();
                out.close();
                fileUrls.add(fileName);
            } catch (IOException e){
                e.printStackTrace();
                return StateCode.buildCode(BizCode.FAIL);
            }
        }


        if(body1 == null){
            return fileUrls;
        }
        for(BodyPart part : body1.getParent().getBodyParts()){
            InputStream is = part.getEntityAs(InputStream.class);
            ContentDisposition meta = part.getContentDisposition();

            try {
                String fileName = String.format("%s", RandomStringUtils.randomAlphanumeric(15));
                OutputStream out = new FileOutputStream(new File(SAVE_FOLDER + fileName));
                byte[] buffer = new byte[1024];
                int bytes = 0;
                while ((bytes = is.read(buffer)) != -1) {
                    out.write(buffer, 0, bytes);
                }
                out.flush();
                out.close();
                fileUrls.add(fileName);
            } catch (IOException e){
                e.printStackTrace();
                return StateCode.buildCode(BizCode.FAIL);
            }
        }
        if(body2 == null){
            return fileUrls;
        }
        for(BodyPart part : body2.getParent().getBodyParts()){
            InputStream is = part.getEntityAs(InputStream.class);
            ContentDisposition meta = part.getContentDisposition();

            try {
                String fileName = String.format("%s", RandomStringUtils.randomAlphanumeric(15));
                OutputStream out = new FileOutputStream(new File(SAVE_FOLDER + fileName));
                byte[] buffer = new byte[1024];
                int bytes = 0;
                while ((bytes = is.read(buffer)) != -1) {
                    out.write(buffer, 0, bytes);
                }
                out.flush();
                out.close();
                fileUrls.add(fileName);
            } catch (IOException e){
                e.printStackTrace();
                return StateCode.buildCode(BizCode.FAIL);
            }
        }

        return fileUrls;
    }
}
